<script setup>
import PageContainer from "@/components/PageContainer.vue";
import { articleChannelService } from "@/api/article";
import { ref } from "vue";
import { Edit, Delete } from "@element-plus/icons-vue";
import ChannelEdit from "./components/ChannelEdit.vue";
import { delArticleChannelService } from "@/api/article";
import { ElMessage } from "element-plus";

const channelList = ref([]);
const loading = ref(false);
const dialog = ref();

const getChannelList = async () => {
  loading.value = true;
  const {
    data: { data },
  } = await articleChannelService();
  channelList.value = data;
  loading.value = false;
};
getChannelList();

const addChannel = () => {
  dialog.value.open({});
};
const onEdit = (row, index) => {
  dialog.value.open(row);
};
const onDelete = async ({ id }) => {
  await ElMessageBox.confirm("您确认删除此条数据？", "Warning", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  });
  await delArticleChannelService(id); //若点了取消则是进入catch报错，否则根据id发送请求去删
  ElMessage.success("删除成功");
  getChannelList(); //删完重新渲染
};
const handleSuccess = () => {
  getChannelList();
};
</script>

<template>
  <PageContainer title="文章分类">
    <template #extra>
      <el-button type="primary" @click="addChannel">添加分类</el-button>
    </template>
    <el-table :data="channelList" stripe v-loading="loading" border>
      <el-table-column type="index" label="序号" width="250" />
      <el-table-column prop="cate_alias" label="别名" width="420" />
      <el-table-column prop="cate_name" label="名字" width="420" />
      <el-table-column label="操作">
        <template #default="scope">
          <el-button
            type="primary"
            :icon="Edit"
            circle
            @click="onEdit(scope.row)"
          />
          <el-button
            type="danger"
            :icon="Delete"
            circle
            @click="onDelete(scope.row)"
          />
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="数据为空" />
      </template>
    </el-table>
  </PageContainer>

  <ChannelEdit ref="dialog" @success="handleSuccess"></ChannelEdit>
</template>

<style scoped lang="scss"></style>
